package org.example.managere.controller;

import org.example.managere.service.DfcmDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.Map;

/**
 * DFCM数据库通用控制器
 * 用于处理dfcmdata数据库的通用操作
 */
@RestController
@RequestMapping("/api/dfcmdata")
public class DfcmDataController {

    @Autowired
    private DfcmDataService dfcmDataService;

    /**
     * 获取所有表名
     */
    @GetMapping("/tables")
    public ResponseEntity<List<String>> getAllTables() {
        try {
            List<String> tables = dfcmDataService.getAllTables();
            return ResponseEntity.ok(tables);
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, 
                    "获取表列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取表的列信息
     */
    @GetMapping("/tables/{tableName}/columns")
    public ResponseEntity<Map<String, String>> getTableColumns(@PathVariable String tableName) {
        try {
            Map<String, String> columns = dfcmDataService.getTableColumns(tableName);
            return ResponseEntity.ok(columns);
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, 
                    "获取表结构失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询表数据
     */
    @GetMapping("/tables/{tableName}/data")
    public ResponseEntity<Map<String, Object>> getTableData(
            @PathVariable String tableName,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String sort,
            @RequestParam(required = false) String direction,
            @RequestParam(required = false) String keyword) {
        
        try {
            Map<String, Object> data = dfcmDataService.getTableData(
                    tableName, page, size, sort, direction, keyword);
            return ResponseEntity.ok(data);
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, 
                    "查询表数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取表的主键列名
     */
    @GetMapping("/tables/{tableName}/primary-key")
    public ResponseEntity<String> getPrimaryKeyColumn(@PathVariable String tableName) {
        try {
            String primaryKey = dfcmDataService.getPrimaryKeyColumn(tableName);
            return ResponseEntity.ok(primaryKey);
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, 
                    "获取主键信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据主键获取记录详情
     */
    @GetMapping("/tables/{tableName}/records")
    public ResponseEntity<Map<String, Object>> getRecordByPrimaryKey(
            @PathVariable String tableName,
            @RequestParam String primaryKeyName,
            @RequestParam String primaryKeyValue) {
        
        try {
            Map<String, Object> record = dfcmDataService.getRecordByPrimaryKey(
                    tableName, primaryKeyName, primaryKeyValue);
            
            if (record == null) {
                throw new ResponseStatusException(HttpStatus.NOT_FOUND, 
                        "未找到指定记录");
            }
            
            return ResponseEntity.ok(record);
        } catch (ResponseStatusException e) {
            throw e;
        } catch (Exception e) {
            throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, 
                    "获取记录详情失败: " + e.getMessage());
        }
    }
} 